<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) https://inphp.cc All rights reserved.
// | 该文件源码由INPHP官方提供，使用协议以INPHP官方公告为准。
// +----------------------------------------------------------------------
// | 站点
// +----------------------------------------------------------------------
namespace app\cms\model;

use Inphp\Core\Db\PDO\Model;
use Inphp\Core\Db\Redis;

class SiteModel extends Model
{
    protected string $tableName = "cms_site";

    /**
     * 缓存到Redis
     */
    public static function cache(): array
    {
        $list = [];
        $sites = self::emptyQuery()->get();
        foreach ($sites as $site) {
            if (empty($site["host"])) {
                continue;
            }
            $site["url"] = ($site["https"] == 1 ? "https://" : "http://").$site["host"];
            $list[$site["host"]] = $site;
        }
        Redis::set("CMS_SITES", $list, -1);
        return $list;
    }

    /**
     * 从缓存获取站点列表
     * @return array
     */
    public static function getCache(): array
    {
        $sites = Redis::get("CMS_SITES");
        return !empty($sites) ? $sites : self::cache();
    }
}